package cn.liumouren.jzxd.service.impl;

import cn.liumouren.jzxd.common.utils.PageUtils;
import cn.liumouren.jzxd.common.utils.Query;
import cn.liumouren.jzxd.dao.GoodsDao;
import cn.liumouren.jzxd.dao.PictureDao;
import cn.liumouren.jzxd.entity.Goods;
import cn.liumouren.jzxd.service.GoodsService;
import cn.liumouren.jzxd.utils.mapperUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("goodsService")
public class GoodsServiceImpl extends ServiceImpl<GoodsDao, Goods> implements GoodsService {

    @Resource
    private PictureDao pictureDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<Goods> page = this.page(
                new Query<Goods>().getPage(params),
                new QueryWrapper<Goods>()
        );
        mapperUtils.getUrlsByEntity(page, pictureDao);
        return new PageUtils(page);
    }

    @Override
    public List<Goods> findByUserId(String userId) {
        List<Goods> goodsList = super.list(new QueryWrapper<Goods>().lambda()
                .eq(Goods::getUserId, userId));

        return mapperUtils.getUrlsByEntity(goodsList, pictureDao);
    }

    @Override
    public IPage<Goods> queryPageByKey(String key, int cur, int size) {
        if (StringUtils.isBlank(key)) {
            return null;
        }
        Page<Goods> page = baseMapper.selectPage(new Page<>(cur, size), new QueryWrapper<Goods>().lambda()
                .like(Goods::getName, key)
                .or()
                .like(Goods::getDescription, key)
                .orderByDesc(Goods::getHot));
        return mapperUtils.getUrlsByEntity(page, pictureDao);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean save(Goods entity) {
        entity.setId(IdWorker.getIdStr());
        entity.setAddTime(new Date());
        entity.setFollower(0);
        mapperUtils.associatePicturesAndEntity(entity, pictureDao);
        return SqlHelper.retBool(baseMapper.insert(entity));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateById(Goods entity) {
        return SqlHelper.retBool(baseMapper.updateById(entity));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeByIds(Collection<? extends Serializable> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return false;
        }
        return SqlHelper.retBool(baseMapper.deleteBatchIds(idList));
    }

    @Override
    public Goods getById(Serializable id) {
        Goods goods = super.getById(id);
        mapperUtils.getUrlsByEntity(goods, pictureDao);
        return goods;
    }
}